package com.kye.express.business.invoice_online.detail;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.invoice_online.ElectronicInvoiceResultBean;
import com.kye.model.bean.invoice_online.GetInvoiceDetailResultBean;
import com.kye.model.bean.invoice_online.RequestElectronicInvoiceBean;
import com.kye.model.bean.invoice_online.RequestGetInvoiceDetailBean;
import com.kye.model.bean.user.User;
import com.kye.model.service.InvoiceOnlineService;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface InvoiceDetailFragmentContract {

    interface InvoiceDetailFragmentView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(GetInvoiceDetailResultBean datas);

        void onApiError(String msg);

        void onOtherError(String msg);
    }

    class InvoiceDetailFragmentViewPresenter extends BasePresenter<InvoiceDetailFragmentContract.InvoiceDetailFragmentView> {

        private Context mContext;

        public InvoiceDetailFragmentViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 查询发票详情
         */
        public void getInvoiceDetail(String groupCode) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(InvoiceOnlineService.class)
                    .billingDetails(new RequestGetInvoiceDetailBean(phone, groupCode))
                    .compose(getView().<OpenApiResult<GetInvoiceDetailResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<GetInvoiceDetailResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<GetInvoiceDetailResultBean> result, GetInvoiceDetailResultBean datas) {
                            if (datas != null) {
                                getView().onLoaded(datas);
                            }else {
                                getView().onOtherError(mContext.getString(R.string.common_no_datas));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<GetInvoiceDetailResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onApiError(msg);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }


    }
}
